package com.tsd.system.controller;

import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.system.entity.SysParameter;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysParameterService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 描述：SysParameter 控制层
 *
 * @author Hillpool
 * @date 2019/03/18 17:43:03
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
public class SysParameterController extends SystemBaseController {

    private final SysParameterService sysParameterService;

    @Autowired
    public SysParameterController(SysParameterService sysParameterService) {
        this.sysParameterService = sysParameterService;
    }

    @ApiOperation(value = "保存系统参数")
    @ResponseBody
    @RequestMapping(value = "/saveSysParameter.action", method = RequestMethod.POST)
    public HttpResult<String> saveSysParameter() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            SysUser sysUser = this.getSysUser();
            Map<String, Object> map = super.getParams(request);
            if (!map.isEmpty()) {
                Set<String> keys = map.keySet();
                for (String name : keys) {
                    String value = HlpUtils.getString(map, name);
                    SysParameter parameter = new SysParameter();
                    parameter.setName(name);
                    parameter.setValue(value);
                    sysParameterService.saveSysParameter(parameter, sysUser.getSid());
                }
            }
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "查询所有系统参数")
    @ResponseBody
    @RequestMapping(value = "/findAllParameters.action", method = RequestMethod.POST)
    public HttpResult<List<SysParameter>> findAllParameters() {
        HttpResult<List<SysParameter>> hr = new HttpResult<List<SysParameter>>();
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            List<SysParameter> list = sysParameterService.findSysParameters(params);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "保存system_开头的系统参数")
    @ResponseBody
    @RequestMapping(value = "/findSystemParameter.action", method = RequestMethod.POST)
    public HttpResult<Object> findSystemParameter() {
        HttpResult<Object> hr = new HttpResult<Object>();
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("nameLike", "system_%");
            List<SysParameter> list = sysParameterService.findSysParameters(params);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
